<template>
  <el-card class="box-card">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="postInfo.postIndex"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="postInfo.postSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <template>
      <el-table :data="postList" stripe style="width: 100%">
        <el-table-column prop="id" label="Id" width="80"> </el-table-column>
        <el-table-column label="略缩图" width="180">
          <template slot-scope="scope">
            <img :src="scope.row.cover[0].url" alt="" class="PostImg" />
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="user.nickname" label="作者"> </el-table-column>
      </el-table>
    </template>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      postList: [],
      postInfo: {
        postIndex: 1,
        postSize: 2
      },
      total: 0
    }
  },
  created() {
    this.getPostList()
  },
  methods: {
    getPostList() {
      this.$axios({
        url: '/post',
        params: {
          pageIndex: this.postInfo.postIndex,
          pageSize: this.postInfo.postSize
        }
      }).then(res => {
        console.log(res.data)
        this.postList = res.data.data
        this.total = res.data.total
      })
    },
    handleSizeChange(newSize) {
      this.postInfo.postSize = newSize
      this.getPostList()
    },
    handleCurrentChange(newIndex) {
      this.postInfo.postIndex = newIndex
      this.getPostList()
    }
  }
}
</script>

<style>
.PostImg {
  width: 234px;
  height: 136px;
  object-fit: cover;
}
</style>
